<template>
  <div class="user-data">
    <div class="title">个人资料</div>
    <el-form :model="user" ref="form" size="medium">
      <el-form-item
        ><div class="item">
          <div class="tit">当前头像：</div>
          <div class="input update">
            <div class="image"><FileImage :file="user.header_img" /></div>
            <!-- <el-button @click="selectImage" size="small" class="button"
              >点击更换</el-button
            > -->
          </div>
        </div>
      </el-form-item>
      <el-form-item
        ><div class="item">
          <div class="tit">昵称：</div>
          <div class="input">
            <el-input v-model="user.wechat_name"></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item v-if="false"
        ><div class="item">
          <div class="tit">真实姓名：</div>
          <div class="input">
            <el-input v-model="user.name"></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item v-if="false"
        ><div class="item">
          <div class="tit">性别：</div>
          <div class="input">
            <el-radio-group v-model="user.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </div>
        </div>
      </el-form-item>
      <el-form-item v-if="false"
        ><div class="item">
          <div class="tit">居住地址：</div>
          <div class="input">
            <el-input v-model="user.addr"></el-input>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <el-button
      style="width: 160px; margin-top: 20px"
      type="primary"
      @click="editUserInfo"
      >保存</el-button
    >
  </div>
</template>

<script>
import api from "@/api";
import { mapState } from "vuex";
import FileImage from "@/components/ImageView/FileImage.vue";
export default {
  name: "UserData",
  components: { FileImage },
  computed: {
    ...mapState("user", ["userinfo"]),
  },
  data() {
    return {
      user: {},
    };
  },
  watch: {
    userinfo() {
      if (this.userinfo) this.user = this.userinfo;
    },
  },
  created() {
    this.user = this.userinfo || {};
    console.log(api);
  },
  methods: {
    selectImage() {
      let input = document.createElement("input");
      input.type = "file";
      input.onchange = () => {
        this.user.header_img = input.files[0];
        console.log(input.files[0]);
      };
      input.click();
    },
    async editUserInfo() {
      let data = {
        id: this.user.user_id,
        phone: this.user.phone,
        wechat_name: this.user.wechat_name,
      };
      await api.EDIT_USERINFO(data);
      this.$message.success("修改成功");
      delete data.id;
      this.$store.commit("user/set", data);
    },
  },
};
</script>

<style lang="scss" scoped>
.user-data {
  border: 1px solid #dedede;
  padding: 20px;
  .title {
    margin-bottom: 28px;
    font-size: 20px;
    font-weight: 600;
    color: #333333;
    line-height: 28px;
  }
  .item {
    display: flex;
    .tit {
      width: 70px;
      font-size: 14px;
      color: #333333;
      line-height: 36px;
      margin-right: 12px;
    }
    .input {
      width: 260px;
      &.update {
        width: 96px;
        flex-direction: column;
        display: flex;
        justify-content: center;
        align-items: center;
        .image {
          width: 70px;
          height: 70px;
          margin: 0 auto 10px;
          background: #ffffff;
          border: 1px solid #c7c7c7;
          border-radius: 50%;
          overflow: hidden;
        }
        .button {
        }
      }
    }
  }
}
</style>
